package com.team9.fitness.controller;

import com.team9.fitness.entity.Diet;
import com.team9.fitness.service.ZRDDSSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;

@RestController
@Slf4j
@RequestMapping("/api/diet")
@CrossOrigin(origins = { "*" }, allowCredentials = "false")
public class DietController {

    @Autowired
    private ZRDDSSendService zrddsSendService;

    /**
     * 保存饮食计划
     */
    @PostMapping("/save/{id}/{diets}")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> save(
            @PathVariable Long id, @PathVariable List<Diet> diets) {

        log.info("保存饮食计划请求: id={}, dietsCount={}", id, diets != null ? diets.size() : 0);

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("id", id);
        requestBody.put("diets", diets);

        return zrddsSendService.sendBusinessRequest("DIET_SAVE", "POST", "/api/diet/save",
                id.toString(), requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "饮食计划保存成功");
                        result.put("data", response.getData());
                        log.info("饮食计划保存成功: id={}", id);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "保存失败");
                        log.warn("饮食计划保存失败: id={}, error={}", id, response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("饮食计划保存异常: id={}", id, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "保存服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 获取所有饮食计划
     */
    @GetMapping("/getAll")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> getAll() {

        log.info("获取所有饮食计划请求");

        return zrddsSendService.sendBusinessRequest("DIET_GET_ALL", "GET", "/api/diet/getAll",
                null, new HashMap<>())
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "获取饮食计划成功");
                        result.put("data", response.getData());
                        log.info("获取饮食计划成功");
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "获取失败");
                        log.warn("获取饮食计划失败: error={}", response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("获取饮食计划异常", throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "获取服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }
}
